package com.shankungfu.adapter;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.shankungfu.R;

public class PickItemDetailAdapter extends BaseAdapter {

	private Context context;
	private List<String> list;
	private DisplayImageOptions options;
	private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
	private ImageLoader imageLoader;

	public PickItemDetailAdapter(Context context, DisplayImageOptions options) {
		this.context = context;
		this.options = options;
		
		
//		BitmapDisplayer displayer = new bitma
//		ImageSize imageSize = new ImageSize(100, 100);
		
		this.options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.default_icon)
		.showImageForEmptyUri(R.drawable.default_icon)
		.showImageOnFail(R.drawable.default_icon)
		.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
		.cacheInMemory(true)
		.cacheOnDisc(true)
		.considerExifParams(false)
		.build();
		
		imageLoader = ImageLoader.getInstance();
	}

	public void setData(List<String> list) {
		this.list = list;
	}

	@Override
	public int getCount() {
		return list != null ? list.size() : 0;
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = View.inflate(context,
					R.layout.pickitem_detail_pic_layout, null);
			holder.pic = (ImageView) convertView.findViewById(R.id.image);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		String pic_path = list.get(position);
		
//		if (pic_path.contains("http")) {
//			imageLoader.displayImage(pic_path, holder.pic, options,
//					animateFirstListener);
//		} else {
//			final ImageView imageView = holder.pic;
			loadLocalPic(pic_path, holder.pic);
//		}
		return convertView;
	}

	private void loadLocalPic(String pic_path, final ImageView imageView) {
		ImageSize imageSize = new ImageSize(80, 80);
		
		imageLoader.loadImage( pic_path, imageSize, new ImageLoadingListener() {
			
			@Override
			public void onLoadingStarted(String imageUri, View view) {
				imageView.setImageResource(R.drawable.default_icon);
			}
			
			@Override
			public void onLoadingFailed(String imageUri, View view,
					FailReason failReason) {
				imageView.setImageResource(R.drawable.default_icon);
			}
			
			@Override
			public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
				imageView.setImageBitmap(loadedImage);				
			}
			
			@Override
			public void onLoadingCancelled(String imageUri, View view) {
				imageView.setImageResource(R.drawable.default_icon);				
			}
		});
	}

	private class ViewHolder {
		private ImageView pic;
	}

	private static class AnimateFirstDisplayListener extends
			SimpleImageLoadingListener {

		static final List<String> displayedImages = Collections
				.synchronizedList(new LinkedList<String>());

		@Override
		public void onLoadingComplete(String imageUri, View view,
				Bitmap loadedImage) {
			if (loadedImage != null) {
				ImageView imageView = (ImageView) view;
				boolean firstDisplay = !displayedImages.contains(imageUri);
				if (firstDisplay) {
					FadeInBitmapDisplayer.animate(imageView, 500);
					displayedImages.add(imageUri);
				}
			}
		}
	}
}
